package com.disapand.strongarmy.controller

import cn.dev33.satoken.secure.*
import cn.dev33.satoken.stp.*
import com.disapand.strongarmy.enum.*
import com.disapand.strongarmy.service.*
import com.disapand.strongarmy.utils.*
import com.disapand.strongarmy.vo.*
import org.hibernate.validator.constraints.*
import org.springframework.format.annotation.*
import org.springframework.validation.annotation.*
import org.springframework.web.bind.annotation.*
import java.time.*

/**
 *
 * @auth disapand
 * @date 2021/10/16 - 22:16 - 星期六
 *
 */
@RestController
@RequestMapping("/accounts", name = "账号管理")
@Validated
class AccountController(private val accountService: AccountService) {

    /**
     * 登录接口
     *
     * @param account
     * @param password
     * @return
     */
    @PostMapping("/login", name = "登录")
    fun login(
        @RequestParam("account") account: String,
        @RequestParam("password") password: String
    ): ResponseVO {
        val admin = accountService.getByPassword(account)
        return if (admin != null) {
            val encryptPassword = SaSecureUtil.sha1(password)
            if (encryptPassword == admin.password) {
                StpUtil.login(admin.id)
                ResponseVO.ofSuccess(StpUtil.getTokenValue())
            } else {
                ResponseVO(ResponseCode.ACCOUNT_OR_PASSWORD_ERROR, "密码错误")
            }
        } else {
            ResponseVO(ResponseCode.ACCOUNT_OR_PASSWORD_ERROR, "账号错误")
        }
    }

    /**
     * 账号注销登录
     */
    @get:PostMapping("/logout", name = "注销登录")
    val logout: ResponseVO
        get() {
            StpUtil.logout(StpUtil.getLoginIdAsInt())
            return ResponseVO.ofSuccess()
        }

    /**
     * 新增用户
     *
     * @param account
     * @param password
     * @param username
     * @param avatar
     * @return
     */
    @PostMapping("/add", name = "新增用户")
    fun add(
        @RequestParam("account") account: String,
        @Length(min = 6, max = 18, message = "密码长度必须大于 6 小于 18") @RequestParam("password") password: String,
        @RequestParam("username", required = false, defaultValue = "") username: String,
        @RequestParam("avatar", required = false, defaultValue = "") avatar: String
    ): ResponseVO {
        val admin = accountService.add(account, password, username, avatar)
        return if (admin) {
            ResponseVO.ofSuccess("账号添加成功")
        } else {
            ResponseVO.ofSuccess("账号添加失败")
        }
    }

    /**
     * 根据 id 获取账号
     *
     * @param id
     * @return
     */
    @GetMapping("/get", name = "获取账号")
    fun get(
        @RequestParam("id") id: Int
    ): ResponseVO {
        val admin = accountService.get(id)
        return if (admin == null) {
            ResponseVO(ResponseCode.NOT_FOUND, "账号不存在")
        } else {
            ResponseVO.ofSuccess(admin)
        }
    }

    /**
     * 获取当前账号的所有菜单，并以树形结构返回
     * 当 id 为空时，获取当前登录账号的所有菜单
     */
    @GetMapping("/menus", name = "树形获取账号菜单")
    fun menus(
        @RequestParam("id", required = false) id: Int?
    ): ResponseVO {
        val result = if (id != null) {
            accountService.getMenus(id)
        } else {
            accountService.getMenus(StpUtil.getLoginIdAsInt())
        }
        return ResponseVO.ofSuccess(SaUtils.generateMenuTree(result.toList()))
    }

    /**
     * 分页获取账号
     *
     * @param current
     * @param pageSize
     * @return
     */
    @GetMapping("/page", name = "分页获取账号列表")
    fun accounts(
        @RequestParam("account", required = false) account: String?,
        @RequestParam("username", required = false) username: String?,
        @RequestParam("status", required = false) status: IntArray?,
        @RequestParam("created_by", required = false) createdBy: Int?,
        @RequestParam("created_at", required = false) createdAt: String?,
        @RequestParam("created_start_at", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") createdStartAt: LocalDateTime?,
        @RequestParam("created_end_at", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") createdEndAt: LocalDateTime?,
        @RequestParam("updated_by", required = false) updatedBy: Int?,
        @RequestParam("updated_at", required = false) updatedAt: String?,
        @RequestParam("updated_start_at", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") updatedStartAt: LocalDateTime?,
        @RequestParam("updated_end_at", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") updatedEndAt: LocalDateTime?,
        @RequestParam("current", required = false, defaultValue = "1") current: Int,
        @RequestParam("page_size", required = false, defaultValue = "20") pageSize: Int
    ): ResponseVO {
        return ResponseVO.ofSuccess(accountService.list(
            account,
            username,
            status,
            createdBy,
            createdAt,
            createdStartAt,
            createdEndAt,
            updatedBy,
            updatedAt,
            updatedStartAt,
            updatedEndAt,
            current,
            pageSize
        ))
    }

    /**
     * 获取所有账号
     */
    @get:GetMapping("/list")
    val list: ResponseVO
        get() {
            return ResponseVO.ofSuccess(accountService.list())
        }
}